package com.allwees.bs.module.mms.rest;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.allwees.bs.core.modelbase.vo.R;
import com.allwees.bs.module.core.dynamic.annotation.DynamicInject;
import com.allwees.bs.module.core.rest.BoCrudApi;
import com.allwees.bs.module.mms.dto.RetailerDTO;
import com.allwees.bs.module.mms.entity.Retailer;
import com.allwees.bs.module.mms.entity.Store;
import com.allwees.bs.module.mms.req.GeneralReq;
import com.allwees.bs.module.mms.req.MerchantDisableReq;
import com.allwees.bs.module.mms.req.MerchantEnableReq;
import com.allwees.bs.module.mms.req.query.RetailerQueryReq;
import com.allwees.bs.module.mms.service.RetailerService;
import com.allwees.bs.module.mms.service.StoreService;
import com.allwees.core.common.util.reflect.PojoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author yangylsky
 * @since 2019-12-03
 */
@RestController
@RequestMapping("/backstage/api/mms/retailer")
public class RetailerApi extends BoCrudApi<Retailer, RetailerService> {

	@Autowired
	private StoreService storeService;

	@DynamicInject(type = DynamicInject.Type.Category,key = "categoryNo")
    @GetMapping("/page")
    public R page(Page<Retailer> page, RetailerQueryReq req) {
		return R.ok(boService.selectPage(page,req));
    }

    @Override
	protected Object _doRetrieve(String uuid) {
		Retailer retailer = boService.findByUuid(uuid);
		RetailerDTO dto = new RetailerDTO();
		PojoUtil.copy(dto, retailer);
		Store store = storeService.selectByRetailerUuid(retailer.getUuid());
		dto.setMerchantType(store.getMerchantType());
		dto.setCategoryNo(store.getCategoryNo());
		return dto;
	}

	/**
	 * 禁用商户
	 *
	 * @param req
	 * @return
	 */
	@PutMapping("/disable")
	public R disable(@RequestBody MerchantDisableReq req) {
		boService.disableRetailers(req);
		return R.ok("执行成功");
	}

	/**
	 * 启用商户
	 *
	 * @param req
	 * @return
	 */
	@PutMapping("/enable")
	public R enable(@RequestBody MerchantEnableReq req) {
		boService.enableRetailers(req);
		return R.ok("执行成功");
	}

	/**
	 * 商户信息
	 *
	 * @param req
	 * @return
	 */
	@GetMapping("/info")
	public R info(GeneralReq req) {
		return super.retrieve(req.getBranchUserUuid());
	}
}
